package leetcode.datastructure.stack;

import java.util.*;

public class Program20Office {

    public Program20Office() {
    }

    public boolean isValid(String s) {
        Deque<Character> stack = new ArrayDeque<>();
        for(int i=0; i<s.length(); i++){
            char cur = s.charAt(i);
            if (cur == ')') {
                if (stack.isEmpty() || stack.pop() != '(') {
                    return false;
                }
            } else if (cur == '}') {
                if (stack.isEmpty() || stack.pop() != '{') {
                    return false;
                }
            } else if (cur == ']') {
                if (stack.isEmpty() || stack.pop() != '[') {
                    return false;
                }
            } else {
                stack.push(cur);
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        Program20Office myStack = new Program20Office();
        System.out.println(myStack.isValid("()")); // 返回 true
        System.out.println(myStack.isValid("()[]{}")); // 返回 true
        System.out.println(myStack.isValid("(]")); // 返回 false
        System.out.println(myStack.isValid("(){}}{")); // 返回 false
    }
}
